package com.ruoyi.cms.controller;

import com.ruoyi.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cms.domain.CmsTopic;
import com.ruoyi.cms.domain.dto.CmsTopicDTO;
import com.ruoyi.cms.service.ICmsTopicService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.HTExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 内容信息Controller
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@Api(tags = "内容信息")
@Controller
@RequestMapping("/cms/topic")
public class CmsTopicController extends BaseController {

    private final String prefix = "cms/topic";

    @Autowired
    private ICmsTopicService cmsTopicService;

    @RequiresPermissions("cms:topic:view")
    @GetMapping()
    public String topic() {
        return prefix + "/topic";
    }

    /**
     * 查询内容信息列表
     */
    @ApiOperation("查询内容信息列表")
    @RequiresPermissions("cms:topic:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo<CmsTopic> list(CmsTopicDTO cmsTopic) {
        startPage();
        List<CmsTopic> list = cmsTopicService.selectCmsTopicList(cmsTopic.as());
        return getDataTable(list);
    }

    /**
     * 导出内容信息列表
     */
    @ApiOperation(value="导出内容信息列表", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @RequiresPermissions("cms:topic:export")
    @Log(title = "内容信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult<?> export(CmsTopicDTO cmsTopic) {
        List<CmsTopic> list = cmsTopicService.selectCmsTopicList(cmsTopic.as());
        return HTExcelUtil.exportExcel(CmsTopic.class, list, queryVisibleFields());
    }

    /**
     * 新增内容信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存内容信息
     */
    @ApiOperation("新增保存内容信息")
    @RequiresPermissions("cms:topic:add")
    @Log(title = "内容信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult<?> addSave(@Validated CmsTopicDTO cmsTopic) {
        cmsTopic.setTopicId(null);
        cmsTopic.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(cmsTopicService.insertCmsTopic(cmsTopic.as()));
    }

    /**
     * 修改内容信息
     */
    @GetMapping("/edit/{topicId}")
    public String edit(@PathVariable("topicId") Long topicId, ModelMap mmap)
    {
        CmsTopic cmsTopic = cmsTopicService.selectCmsTopicById(topicId);
        mmap.put("cmsTopic", cmsTopic);
        return prefix + "/edit";
    }

    /**
     * 修改保存内容信息
     */
    @ApiOperation("修改保存内容信息")
    @RequiresPermissions("cms:topic:edit")
    @Log(title = "内容信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult<?> editSave(@Validated CmsTopicDTO cmsTopic) {
        if (null == cmsTopic.getTopicId()) {
            return AjaxResult.error("ID不能为空");
        }
        cmsTopic.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(cmsTopicService.updateCmsTopic(cmsTopic.as()));
    }

    /**
     * 删除内容信息
     */
    @ApiOperation("删除内容信息")
    @ApiImplicitParam(name = "ids", value = "内容信息ID.多个ID间以英文逗号隔开", dataType = "String", paramType = "query", required = true)
    @RequiresPermissions("cms:topic:remove")
    @Log(title = "内容信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult<?> remove(String ids) {
        return toAjax(cmsTopicService.deleteCmsTopicByIds(ids));
    }
}
